<template>
    <el-dialog
        title="修改登录密码"
        :visible="visible"
        append-to-body
        :destroy-on-close="true"
        @close="close"
    >
        <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            size="small"
        >
            <el-form-item label="新密码" prop="password">
                <el-input
                    type="password"
                    v-model="ruleForm.password"
                    autocomplete="off"
                ></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="repet_password">
                <el-input
                    type="password"
                    v-model="ruleForm.repet_password"
                    autocomplete="off"
                ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')" :disabled="isDisabled" :loading="isLoading"
                    >提交</el-button
                >
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import { adminEditPwd } from "@/api/staff.js";
export default {
    name: "editPermission",
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            } else {
                if (this.ruleForm.repet_password !== "") {
                    this.$refs.ruleForm.validateField("repet_password");
                }
                callback();
            }
        };
        var validateNewPass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.ruleForm.password) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            visible: false,
            isDisabled:false,
            isLoading:false,
            ruleForm: {
                id: 0,
                password: "",
                repet_password: "",
            },
            rules: {
                password: [{ validator: validatePass, trigger: "blur" }],
                repet_password: [
                    { validator: validateNewPass, trigger: "blur" },
                ],
            },
        };
    },
    methods: {
        show() {
            this.visible = true;
        },
        async submitForm(formName) {
            this.isDisabled = true;
            this.isLoading = true;
            let res = await this.$getCsrToken();
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (res.code == 0) {
                        // localStorage.setItem("X-Csrf-Token", res.token);
                        adminEditPwd(this.ruleForm).then((res) => {
                            if (res.code == 0) {
                                this.$message({
                                    message: res.msg,
                                    type: "success",
                                });
                                this.close();
                                this.isDisabled = false;
                                this.isLoading = false;
                            }else{
                                this.isDisabled = false;
                                this.isLoading = false;
                            }
                        });
                    }
                } else {
                    return false;
                }
            });
        },
        close() {
            this.visible = false;
        },
    },
};
</script>